﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrHieuChinh : Form
    {
        public FrHieuChinh()
        {
            InitializeComponent();
        }
        private string string_temp, SLCu1, SLMoi1, KhuVuc1, TenSP1, MaCT1, TenCT1,child=" - ",SLHC1;
        private double SLUpdate = 0;
        private int i = 0;
        private void Search()
        {
            Process prs = new Process();
            DataSet ds = new DataSet();
            if (cb_MaSP.Text != "" && cb_KhuVuc.Text != "")
            {
                try
                {
                    if (cb_KhuVuc.Text == "DinhVi")
                    {
                        string_temp = @"select dbo.TonDinhVi.TenSP,TonDinhVi.MaChiTiet,TonDinhVi.TenChiTiet,TonDinhVi.SLHCKV2*SoLuongCT as SoLuong, TonDinhVi.TonMBKV2*SoLuongCT as TonMB,KhuVuc='DinhVi'
                            from dbo.TonDinhVi Where MaChiTiet like N'" + prs.substring_right(cb_MaSP.Text, child) + "%'";
                    }
                    if (cb_KhuVuc.Text == "LapRap")
                    {
                        string_temp = @"select dbo.TonLapRap.TenSP,TonLapRap.MaChiTiet,TonLapRap.TenChiTiet,TonLapRap.SLHCKV3 as SoLuong,isnull(TonLapRap.TonMBLR,0) as TonMB,KhuVuc='LapRap'
                        from dbo.TonLapRap where MaChiTiet like N'" + prs.substring_right(cb_MaSP.Text, child) + "%'";
                    }
                    if (cb_KhuVuc.Text == "SauNhung")
                    {
                        string_temp = @"select TonSauNhung.TenSP,TonSauNhung.MaChiTiet,TonSauNhung.TenChiTiet,TonSauNhung.SLHCKV4 as SoLuong,TonSauNhung.TonMBKV4 as TonMB,KhuVuc='SauNhung' from dbo.TonSauNhung
                                        where MaChiTiet like '"+prs.substring_right(cb_MaSP.Text,child)+"%'";
                    }
                    if (dataGridView1.Rows.Count > 1)
                        dataGridView1.Rows.Clear();
                    ds = prs.dts(string_temp);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            TenSP1 = ds.Tables[0].Rows[i]["TenSP"].ToString();
                            MaCT1 = ds.Tables[0].Rows[i]["MaChiTiet"].ToString();
                            TenCT1 = ds.Tables[0].Rows[i]["TenChiTiet"].ToString();
                            SLHC1 = ds.Tables[0].Rows[i]["SoLuong"].ToString();
                            SLCu1 = ds.Tables[0].Rows[i]["TonMB"].ToString();
                            SLMoi1 = "0";
                            KhuVuc1 = ds.Tables[0].Rows[i]["KhuVuc"].ToString();
                            dataGridView1.Rows.Add(false, TenSP1, MaCT1, TenCT1, SLHC1, SLCu1, SLMoi1, KhuVuc1);
                        }
                        prs.formatcolordatagrid(dataGridView1);

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    ds.Clear();
                }
            }
            else
                MessageBox.Show("Nhập tên Sản Phẩm và khu vực");
        }

        private void bt_Search_Click(object sender, EventArgs e)
        {
            Search();
        }
        private void edit()
        {
            Data dta = new Data();
            SqlConnection Conn = dta.Connect();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
             DialogResult d = MessageBox.Show("Tất cả những dòng dữ liệu được check sẽ được sửa", "Thông Báo", MessageBoxButtons.OKCancel);
             if (d == DialogResult.OK)
             {
                 try
                 {

                     if (lb_user.Text == "administrator")
                     {
                         string check;
                         while (dataGridView1.Rows.Count > 1)
                         {
                             check = dataGridView1.Rows[0].Cells["checkbox"].Value.ToString();
                             //MessageBox.Show(check);
                             if (check == "1" || check == "True")
                             {
                                 MaCT1 = dataGridView1.Rows[0].Cells["MaCT"].Value.ToString();
                                 KhuVuc1 = dataGridView1.Rows[0].Cells["KhuVuc"].Value.ToString();
                                 SLMoi1 = dataGridView1.Rows[0].Cells["SLMoi"].Value.ToString();
                                 SLHC1 = dataGridView1.Rows[0].Cells["SLHC"].Value.ToString();
                                 SLCu1 = dataGridView1.Rows[0].Cells["SoLuong"].Value.ToString();
                                 SLUpdate = double.Parse(SLHC1) + double.Parse(SLCu1) - double.Parse(SLMoi1);
                                 //MessageBox.Show(SLUpdate.ToString());
                                 string_temp = @"UPDATE [dbo].[HieuChinhSL] SET [SoLuong] ="+SLUpdate.ToString()+" WHERE KhuVuc=N'"+KhuVuc1+"' and MaCT='"+MaCT1+"'";
                                 cmd.CommandText = string_temp;
                                 cmd.ExecuteNonQuery();
                             }
                             dataGridView1.Rows.RemoveAt(0);
                         }
                         MessageBox.Show("Dữ liệu được sửa thành công");
                     }
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
                 finally
                 {
                     Conn.Close();
                     cmd.Dispose();
                 }
             }
        
        }

        private void FrHieuChinh_Load(object sender, EventArgs e)
        {
            Data dta = new Data();
            
            SqlConnection Conn = dta.Connect();
           
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            cmd.CommandText = @"select * from dbo.Load_cbSP";
            try
            {
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    cb_MaSP.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                }
                rd.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            edit();
        }

        private void chk_selectall_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_selectall.Checked == true)
            {
                for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    dataGridView1.Rows[i].Cells["checkbox"].Value = true;
                }
            }
            else
            {
                for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    dataGridView1.Rows[i].Cells["checkbox"].Value = false;
                }
            }
        }
    }
}
